import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}
// 公共路由
export const constantRoutes = [

  {
    name: 'root',
    path: '/',  // 程序启动默认路由
    component: () => import('@/components/common/Whole.vue'),
    meta: { title: '整体页面布局' },
    redirect: '/home',  // 重定向到首页
    hidden: true,
    children: [
      {
        name: '首页',
        path: '/home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' }
      }
    ]
  },
  {
    path: '/Login',  // 登录页面
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' },
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404.vue'),
    meta: { title: '404' },
    hidden: true
  },
  {
    path: '/403',
    component: () => import('@/views/403.vue'),
    meta: { title: '403' },
    hidden: true
  },
]

export default new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
});

// 防止连续点击多次路由报错
// let routerPush = Router.prototype.push;
// let routerReplace = Router.prototype.replace;
// // push
// Router.prototype.push = function push(location) {
//   return routerPush.call(this, location).catch(err => err)
// }
// // replace
// Router.prototype.replace = function push(location) {
//   return routerReplace.call(this, location).catch(err => err)
// }
